package com.icexxx.icemock;

import java.util.ArrayList;
import java.util.List;

import com.icexxx.icemock.pojo.Feather;
import com.icexxx.icemock.pojo.Peafowl;
import com.icexxx.icemock.util.MockBattery;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;

public class Main {

	public static void main(String[] args) {
		String path = "E:\\ee.txt";
		if (args.length > 0) {
			path = args[0];
			path = path.replace("\\", "/");
		}
		List<String> readUtf8Lines = FileUtil.readUtf8Lines(path);
		String tableName = null;
		List<Peafowl> peafowlList = new ArrayList<>();
		List<Feather> featherList = new ArrayList<>();
		Peafowl peafowl = null;
		Feather feather = null;
		int count = 0;
		if (CollUtil.isNotEmpty(readUtf8Lines)) {
			for (int i = 0; i < readUtf8Lines.size(); i++) {
				String line = readUtf8Lines.get(i);
				if (StrUtil.isNotBlank(line)) {
					if (line.startsWith("==")) {
						if (peafowl != null) {
							peafowl.setFeather(featherList);
							peafowlList.add(peafowl);
						}
						peafowl = new Peafowl();
						count = MockBattery.analysisCount(line);
						tableName = MockBattery.analysisTableName(line);
						peafowl.setCount(count);
						peafowl.setFeather(featherList);
						peafowl.setTableName(tableName);
						featherList = new ArrayList<>();
					} else if (line.startsWith("//")) {

					} else if (line.startsWith("-- ")) {

					} else {
						feather = MockBattery.analysisFeather(line);
						featherList.add(feather);
					}
				}
			}
			if (peafowl != null) {
				peafowl.setFeather(featherList);
				peafowlList.add(peafowl);
			}
		}

		String formatJsonStr = JSONUtil.formatJsonStr(JSONUtil.parseArray(peafowlList).toString());
		System.out.println(formatJsonStr);
		MockBattery.mockData(peafowlList);
	}

}
